package com.zxx.otherchaos.widgets.rvScroll;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zxx.otherchaos.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

/**
 * RecyclerView提供了几种移动的方法:
 *
 * scrollToPosition     （直接闪现至某一位位置）
 * scrollTo
 * scrollBy
 * smoothScrollBy
 * smoothScrollToPosition   （惯性滑动至某一位置）
 *
 * scrollToPositionWithOffset(int position, int offset)
 *  因为他们只保证能够展示出来，并不能保证在第一位。偏移值，传入0就会有你想要的
 * @author zxx on 2021/4/14
 */
public class RvHuaDongActivity extends AppCompatActivity implements View.OnClickListener {

    RecyclerView mRv;
    TextView guanXing1,guanXing2,guanXing3,shanXian1,shanXian2,shanXian3;

    private LinearLayoutManager mManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv_hua_dong);
        mRv = findViewById(R.id.rv_rvhuadong);
        guanXing1 = findViewById(R.id.tv_rvhuadong_GuanXing_1);
        guanXing2 = findViewById(R.id.tv_rvhuadong_GuanXing_2);
        guanXing3 = findViewById(R.id.tv_rvhuadong_GuanXing_3);
        shanXian1 = findViewById(R.id.tv_rvhuadong_ShanXian_1);
        shanXian2 = findViewById(R.id.tv_rvhuadong_ShanXian_2);
        shanXian3 = findViewById(R.id.tv_rvhuadong_ShanXian_3);
        guanXing1.setOnClickListener(this);
        guanXing2.setOnClickListener(this);
        guanXing3.setOnClickListener(this);
        shanXian1.setOnClickListener(this);
        shanXian2.setOnClickListener(this);
        shanXian3.setOnClickListener(this);

        initData();
    }

    private void initData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("position" + i);
        }
        mManager = new LinearLayoutManager(this);

        mRv.setLayoutManager(mManager);
        mRv.setAdapter(new MyAdapter(this, list));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_rvhuadong_GuanXing_1:  //惯性随机滑动
                int position1 = (int) (Math.random() * 100);
                Toast.makeText(this, "滑倒：" + position1, Toast.LENGTH_SHORT).show();

                LinearSmoothScroller smoothScroller = new TopSmoothScroller(getApplicationContext());
                smoothScroller.setTargetPosition(position1);
                mManager.startSmoothScroll(smoothScroller);
                break;

            case R.id.tv_rvhuadong_GuanXing_2:  //惯性滑动到20
                LinearSmoothScroller scroller2 = new TopSmoothScroller(this);
                scroller2.setTargetPosition(20);
                mManager.startSmoothScroll(scroller2);
                break;

            case R.id.tv_rvhuadong_GuanXing_3:  //惯性滑动到99
                LinearSmoothScroller scroller3 = new TopSmoothScroller(getApplication());
                scroller3.setTargetPosition(99);
                mManager.startSmoothScroll(scroller3);
                break;

            case R.id.tv_rvhuadong_ShanXian_1:  //随机闪现
                int position2 = (int) (Math.random() * 100);
                Toast.makeText(this, "闪到：" + position2, Toast.LENGTH_SHORT).show();
                mManager.scrollToPositionWithOffset(position2, 0);

                break;
            case R.id.tv_rvhuadong_ShanXian_2:  //闪现到20
                mManager.scrollToPositionWithOffset(20, 0);
                break;

            case R.id.tv_rvhuadong_ShanXian_3:  //闪现到99
                mManager.scrollToPositionWithOffset(99, 0);
                break;
        }
    }

    private static class TopSmoothScroller extends LinearSmoothScroller {
        public TopSmoothScroller(Context context) {
            super(context);
        }

        @Override
        protected int getHorizontalSnapPreference() {
            return SNAP_TO_START;//具体见源码注释
        }

        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;//具体见源码注释
        }
    }
}
